#include "new.h"
#include <malloc.h>
#include "util/assert.h"
#include "util/callstack.h"
#include <windows.h>
#include <Dbghelp.h>

/*

void InitMemDebug()
{
	SymSetOptions(SYMOPT_LOAD_LINES);
	BOOL initRes = SymInitialize(GetCurrentProcess(), NULL, TRUE);

	if (!initRes)
	{
		FSDebugPrint("SymInitialize failed with error %d", GetLastError());
	}
}

void DestroyMemDebug()
{
	 SymCleanup(GetCurrentProcess());
}

void* _Mem_Alloc( size_t size )
{
	// capture the callstack at this point
	const int kMaxCallers = 62; 

	void* callers[kMaxCallers];
	int count = GetCallstack(kMaxCallers, callers);
	for(int i = 0; i < count; i++)
	{
		// get function name from address ---------------------------------------------------------
		DWORD64 dis64 = 0;
		IMAGEHLP_SYMBOL64 *pSym = NULL;

		char buffer[sizeof(IMAGEHLP_SYMBOL64) + MAX_SYM_NAME*sizeof(TCHAR)] = {0};

		pSym = (IMAGEHLP_SYMBOL64 *) buffer;
		pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
		pSym->MaxNameLength = MAX_PATH;

		BOOL gotSymbolName = SymGetSymFromAddr64(GetCurrentProcess(), (DWORD64)callers[i], &dis64, pSym);
		//-----------------------------------------------------------------------------------------

		// get line number ------------------------------------------------------------------------
		IMAGEHLP_LINE64 line;
		DWORD dis = 0;
		memset(&line, 0, sizeof(line));
		line.SizeOfStruct = sizeof(line);
		BOOL gotSymbolFile = SymGetLineFromAddr64(GetCurrentProcess(), (DWORD64)callers[i], &dis, &line);

		if (gotSymbolName && gotSymbolFile)
		{
			FSDebugPrint("Allocation (%d) [%s (%s, %d)]", size, pSym->Name, line.FileName, line.LineNumber);
		}
		//-----------------------------------------------------------------------------------------
	}

	return malloc(size);
}

void _Mem_Free( void *ptr )
{
	free(ptr);
}
*/